package com.pureut.storage.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pureut.storage.dto.PickingRestDbDto;
import com.pureut.storage.dto.PickingRestHandoverDto;
import com.pureut.storage.dto.SalesReturnDto;
import com.pureut.storage.export.SalesReturnExport;
import com.pureut.storage.service.SalesReturnService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.OrderReviewVo;
import com.pureut.storage.vo.PickingRestDbVo;
import com.pureut.storage.vo.SalesReturnVo;
import com.pureut.storage.vo.UpdateStatusByQualityVo;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/***
 * 销售退货控制层
 * Author:C
 * Date:2022/12/29 11:15
 * @DESC
 */
@RestController
@RequestMapping("/sales-return")
public class SalesReturnController extends BaseController {

    @Resource
    SalesReturnService salesReturnService;


    /**
     * 销售退货列表
     *
     * @param salesReturnVo
     * @return
     */
    //@SaCheckPermission("warehouse:warehousing:saleReturn:list")
    @GetMapping("/list")
    public TableDataInfo<SalesReturnDto> getList(SalesReturnVo salesReturnVo, PageQuery pageQuery) throws Exception {
        //查询委外入库列表
        return salesReturnService.getSalesList(salesReturnVo, pageQuery);
    }


    /**
     * 新增销售退货
     */
    //@SaCheckPermission("warehouse:warehousing:saleReturn:add")
    @Log(title = "新增销售退货", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody SalesReturnVo salesReturnVo) throws Exception {
        return toAjax(salesReturnService.addSalesReceipt(salesReturnVo));
    }

    /**
     * 关结销售退货
     */
    //@SaCheckPermission("warehouse:warehousing:saleReturn:closing")
    @Log(title = "关结销售退货", businessType = BusinessType.UPDATE)
    @PostMapping("/close")
    public R<Void> close(@RequestBody SalesReturnVo salesReturnVo) {
        return toAjax(salesReturnService.close(salesReturnVo));
    }

    /**
     * 查看
     */
    //@SaCheckPermission("warehouse:warehousing:saleReturn:query")
    @GetMapping("/view/{id}")
    public R<SalesReturnDto> view(@PathVariable("id") Long id) {
        return R.ok(salesReturnService.getView(id));
    }

    /**
     * 修改销售退货
     */
    //@SaCheckPermission("warehouse:warehousing:saleReturn:edit")
    @Log(title = "修改销售退货", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public R<Void> updateSalesReceipt(@Validated @RequestBody SalesReturnVo salesReturnVo) {
        return toAjax(salesReturnService.updateSalesReceipt(salesReturnVo));
    }

    /**
     * 删除销售退货
     */
    //@SaCheckPermission("warehouse:warehousing:saleReturn:remove")
    @Log(title = "删除销售退货", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{idStr}")
    public R<Void> deleteSalesReceipt(@PathVariable("idStr") String idStr) {
        return toAjax(salesReturnService.deleteSalesReceipt(idStr));
    }

    /**
     * 提交审核
     */
    //@SaCheckPermission("warehouse:warehousing:saleReturn:submitReview")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/submitReview/{id}")
    public R<Void> submitReview(@PathVariable("id") Long id) {
        return toAjax(salesReturnService.submitReview(id));
    }

    /**
     * 撤销审核
     */
    //@SaCheckPermission("warehouse:warehousing:saleReturn:deleteReview")
    @Log(title = "撤销审核", businessType = BusinessType.UPDATE)
    @GetMapping("/deleteReview/{id}")
    public R<Void> deleteReview(@PathVariable("id") Long id) {
        return toAjax(salesReturnService.deleteReview(id));
    }

    /**
     * 审核
     */
    //@SaCheckPermission("warehouse:warehousing:saleReturn:review")
    @Log(title = "审核单据", businessType = BusinessType.UPDATE)
    @PostMapping("/saleReturnReview")
    public R<Void> saleReturnReview(@Validated @RequestBody OrderReviewVo orderReviewVo) throws Exception {
        return toAjax(salesReturnService.saleReturnReview(orderReviewVo));
    }

    /**
     * 导出
     */
    //@SaCheckPermission("warehouse:warehousing:saleReturn:export")
    @PostMapping("/salesMessageExport")
    public R<Void> salesMessageExport(HttpServletResponse response, SalesReturnVo salesReturnVo) throws IOException {
        List<SalesReturnExport> infoExport = salesReturnService.getInfoExport(salesReturnVo);
        ExcelUtil<SalesReturnExport> excelUtil = new ExcelUtil<SalesReturnExport>(SalesReturnExport.class);
        excelUtil.excelWriter(response, infoExport, "销售退货.xlsx");
        return R.ok();
    }

    /**
     * 销售退货（pda）
     */
    //@SaCheckPermission("warehouse:warehousing:saleReturn:warehousing")
    @PostMapping("/salesReturnPDA")
    @Log(title = "销售退货(pda)", businessType = BusinessType.UPDATE)
    public R<PickingRestDbDto> salesReturnPda(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return R.ok(salesReturnService.salesReturnPda(pickingRestDbVo));
    }

    /**
     * 销售退货拣货撤销
     */
    @PostMapping("/pickingCancellation")
    @Log(title = "销售退货拣货撤销", businessType = BusinessType.UPDATE)
    public R<Void> pickingCancellation(@RequestBody PickingRestDbVo pickingRestDbVo) {
        return toAjax(salesReturnService.pickingCancellation(pickingRestDbVo));
    }

    /**
     * 销售退货拣货完成
     */
    @PostMapping("/pickingCompleted")
    @Log(title = "销售退货拣货完成", businessType = BusinessType.UPDATE)
    public R<Void> pickingCompleted(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return toAjax(salesReturnService.pickingCompleted(pickingRestDbVo));
    }

    /**
     * 销售退货交接
     */
    //@SaCheckPermission("warehouse:warehousing:saleReturn:handover")
    @PostMapping("/handover")
    @Log(title = "销售退货交接", businessType = BusinessType.UPDATE)
    public R<PickingRestHandoverDto> handover(@RequestBody PickingRestDbVo pickingRestDbVo) {
        return R.ok(salesReturnService.handover(pickingRestDbVo));
    }

//    /**
//     * 销售退货品质检验回传修改数据(品质审核时fegin调用)
//     */
//    @PostMapping("/updateStatusByQuality")
//    public AjaxResult updateStatusByQuality(@RequestBody UpdateStatusByQualityVo updateStatusByQualityVo) {
//        return toAjax(salesReturnService.updateStatusByQuality(updateStatusByQualityVo));
//    }
}
